var io = require('socket.io-client');
//var robotInfo = require('./robot').getInstand;
var log = require("./../../util/loginfo").getInstand;
GoldenFlower = function (_userInfo) {
    this._userId = 0;
    this._playCount = Math.floor(Math.random() * 15) + 5;
    this._online = false;
    this._hallSocket = null;
    this._gameSocket = null;
    this._account = null;
    this._sign = null;
    this._socre = 0;
    this._idx = 0;
    this._gameInfo = {};
    this._checkPai = false;
    this._userList = new Set();

    this.init = () => {
        this._idx = _userInfo.idx;
        this._account = _userInfo.account;
        //连接大厅
        this._hallSocket = io.connect('http://localhost:13000');
        this.addHallSocketListeners();
    };

    this.addHallSocketListeners = () => {
        this._hallSocket.on('connected', () => {
            this._hallSocket.emit('login', {
                userName: this._account,
                sign: '@@@@youmegame2019`@112233445566@@@@',
                gameId: 1
            })
        });

        this._hallSocket.on('loginResult', res => {
            // //log.info('^^^loginResult:'+JSON.stringify(res));
            if (res.resultid !== 1) {
                this.disconnect();
                return;
            }
            this._sign = res.Obj.sign;
            this._userId = res.Obj.id;
            this._socre = res.Obj.score;
            this._online = true;
        });

        this._hallSocket.on('ServerListResult', res => {
            log.info(this._userId + "进入炸金花");
            let index = 0;
            for (let i = 0; i < res.GameInfo.length; i++) {
                if (res.GameInfo[i].GameId == 11) {
                    index = i;
                    break;
                }
            }
            this._gameSocket = io.connect('http://' + res.GameInfo[index].serverInfo.normal[this._idx].ip + ':' + res.GameInfo[index].serverInfo.normal[this._idx].prot);
            this.addGoldFlowerSocketListeners();
        })
    };

    this.addGoldFlowerSocketListeners = () => {
        this._gameSocket.on('connected', () => {
            this._gameSocket.emit("LoginGame", {userid: this._userId, gametype: 11, sign: this._sign});
        });

        this._gameSocket.on('loginGameResult', res => {
            // //log.info('^^^^进入砸金花游戏：'+JSON.stringify(res));
            if (res.resultid) {
                this._hallSocket.disconnect();
                this._gameInfo = {};
                this._gameInfo.playerCoin = res.Obj.score;
                this._gameSocket.emit('LoginRoom', {roomid: 1});
            }
        });

        this._gameSocket.on('LoginRoomResult', res => {
            // //log.info('^^^^进入砸金花房间：'+JSON.stringify(res));
            if (res.ResultCode) {
                this._gameInfo.tableId = res.ResultData.TableId;
                this._gameInfo.seatIndex = res.ResultData.seatId;
                this._gameInfo.tax = res.ResultData.tax;
                this._gameInfo.addScore = res.ResultData.addscore;
                this.ready();
            }
        });

        this._gameSocket.on('game_myTurn_push', res => {
            if (!this._checkPai && Math.random() * 100 >= 25) {
                this._checkPai = true;
                this._gameSocket.emit('kanpai');
            }
            setTimeout(() => {
                if (res.allInFlag) {
                    this._gameSocket.emit('all_in');
                    return;
                }
                if (res.canGenzhu && Math.random() * 100 >= 60) {
                    this._gameSocket.emit('genzhu');
                    return;
                }
                let betLsit = [500, 1000, 1500, 2000];
                if (res.canAddZhu && Math.random() * 100 >= 40) {
                    let bet = 0;
                    for (let i in betLsit) {
                        if (res.currentZhu < betLsit[i]) {
                            bet = betLsit[i];
                            break;
                        }
                    }
                    if (bet != 0) {
                        this._gameSocket.emit('addzhu', bet);
                        return;
                    }
                }
                let arr = [...this._userList];
                let index = parseInt(Math.random() * arr.length, 10);
                if (arr[index] != this._userId && res.canBiPai && Math.random() * 100 <= 50) {
                    this._gameSocket.emit('bipai', {
                        userId1: this._userId,
                        userId2: arr[index]
                    });
                    return;
                }
                this._gameSocket.emit('qipai');
            }, Math.floor(Math.random() * 5000));
        });

        this._gameSocket.on('game_oneInWin_push', () => {
            this.ready();
        });

        this._gameSocket.on('login_result', res => {
            for (let i in res.seats) {
                if (res.seats[i].userid != 0) {
                    this._userList.add(res.seats[i].userid);
                }
            }
        });

        this._gameSocket.on('new_user_comes_push', res => {
            this._userList.add(res.userid);
        });

        this._gameSocket.on('exit_notify_push', res => {
            this._userList.delete(res);
        });

        this._gameSocket.on('game_noMoney_exit', () => {
            this.disconnect();
            this._playCount = 0;
        })
    };

    this.ready = () => {
        this._checkPai = false;
        this._gameSocket.emit('ready');
        this._playCount -= 1;
    };

    this.isOnline = () => {
        return this._playCount > 0;
    };

    this.disconnect = () => {
        if (this._hallSocket) {
            this._hallSocket.disconnect();
            this._hallSocket = null;
        }
        if (this._gameSocket) {
            this._gameSocket.disconnect();
            this._gameSocket = null;
        }
        this._online = false;
    };

    this.init();
};

module.exports = GoldenFlower;